package com.yndsoft.jc.supplier.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.yndsoft.jc.R;
import com.yndsoft.jc.supplier.net.ApiService;
import com.yndsoft.jc.supplier.order.OnDeleteListener;
import com.yndsoft.jc.supplier.order.PreviewPictureActivity;
import com.yndsoft.jc.supplier.utils.Constants;
import com.yndsoft.jc.supplier.utils.ScreenUtil;
import com.yndsoft.jc.supplier.utils.UiUtils;
import com.yndsoft.jc.supplier.widget.imageselector.ImageLoader;
import com.yndsoft.jc.supplier.widget.imageselector.ImgSelActivity;
import com.yndsoft.jc.supplier.widget.imageselector.ImgSelConfig;
import com.yndsoft.jc.supplier.widget.recyclerview.RecyclerAdapter;
import com.yndsoft.jc.supplier.widget.recyclerview.RecyclerHolder;
import com.yndsoft.jc.supplier.widget.recyclerview.RecyclerListener;

import java.util.ArrayList;
import java.util.List;

public class ImageAdapter extends RecyclerAdapter<String> implements RecyclerListener.OnClickListener<RecyclerHolder> {

    private Activity activity;
    private ImageLoader loader = new ImageLoader() {
        @Override
        public void displayImage(Context context, String path, ImageView imageView) {
            Glide.with(context)
                    .load(path)
                    .placeholder(R.drawable.ic_default_image)
                    .centerCrop()
                    .into(imageView);
        }
    };
    private List<String> imgList;
    private int realWH;
    private OnDeleteListener onDeleteListener;

    public void setOnDeleteListener(OnDeleteListener onDeleteListener) {
        this.onDeleteListener = onDeleteListener;
    }

    public List<String> getImgList() {
        return imgList;
    }

    public ImageAdapter(Context context, Activity activity, List<String> data) {
        super(context, data, R.layout.item_entry_img);
        this.activity = activity;
        int screenWidth = ScreenUtil.getScreenWidth(this.mContext);
        int dip2Px = UiUtils.dip2Px(mContext, 12);
        realWH = (screenWidth - dip2Px * 2) / 5;
    }

    @Override
    public void convert(RecyclerHolder holder, String model) {
        RelativeLayout entryRoot = holder.findViewById(R.id.entry_root);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT
        );
        params.width = realWH;
        params.height = realWH;
        entryRoot.setLayoutParams(params);
        ImageView entryImg = holder.findViewById(R.id.entry_img);
        ImageView entryDel = holder.findViewById(R.id.entry_del);

        if (model.equals(Constants.PLACE_HOLDER)) {
            Glide.with(mContext).load(R.mipmap.imgadd).centerCrop().into(entryImg);
            entryDel.setVisibility(View.GONE);
        } else {
            Glide.with(mContext).load(ApiService.BASE_URL + model).placeholder(R.mipmap.product_img).centerCrop().into(entryImg);
            entryDel.setVisibility(View.VISIBLE);
        }
        holder.setOnClickListener(this, R.id.entry_img, R.id.entry_del);
    }

    @Override
    public void onClick(View v, int position, RecyclerHolder holder) {
        switch (v.getId()) {
            case R.id.entry_img:
                if (mData != null) {
                    int size = mData.size();
                    String s = mData.get(position);
                    if (s != null) {
                        if (position == size - 1 && s.equals(Constants.PLACE_HOLDER)) {
                            imgList = new ArrayList<>();
                            mData.remove(size - 1);
                            imgList.addAll(mData);
                            ImgSelConfig config = new ImgSelConfig.Builder(mContext, loader)
                                    .multiSelect(false)
                                    .rememberSelected(false)
                                    .needCamera(true)
                                    .maxNum(1)
                                    .build();
                            ImgSelActivity.startActivity(activity, config, Constants.REQUEST_ENTRY_CODE);
                        } else {
                            PreviewPictureActivity.start(mContext, position, (ArrayList<String>) mData);
                        }
                    }
                }
                break;
            case R.id.entry_del:
                if (onDeleteListener != null) {
                    onDeleteListener.onDelete(position);
                    remove(position);
                }
                break;
        }
    }
}
